package com.mrbug.pulltodismisspager.example;

import com.mrbug.pulltodismisspager.PullToDismissPager;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Window;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 首页
 */
public class MainAbility extends Ability {
    private PullToDismissPager pull_to_dismiss_pager;
    private Text tvShow;
    private Text tvHello;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
        Window window = getWindow();
        window.setStatusBarColor(Color.BLACK.getValue());
        window.setStatusBarVisibility(Component.VISIBLE);
        pull_to_dismiss_pager = (PullToDismissPager) findComponentById(ResourceTable.Id_pull_to_dismiss_pager);
        tvHello = (Text) findComponentById(ResourceTable.Id_tv_hello);
        tvShow = (Text) findComponentById(ResourceTable.Id_tv_show);
        tvShow.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!pull_to_dismiss_pager.isPanelExpanded()) {
                    pull_to_dismiss_pager.expandPanel();
                }
            }
        });
        pull_to_dismiss_pager.addView();
        SlidingPagerAdapter pagerAdapter = new SlidingPagerAdapter(this);
        pull_to_dismiss_pager.setProvider(pagerAdapter);
        pull_to_dismiss_pager.setStateChangeLisener(new PullToDismissPager.StateChangeLisener() {
            @Override
            public void isOpenState() {
                tvHello.setEnabled(false);
            }

            @Override
            public void isCloseState() {
                tvHello.setEnabled(true);
            }
        });
    }

    private class SlidingPagerAdapter extends PageSliderProvider {
        public SlidingPagerAdapter(MainAbility mainAbility) {
        }

        @Override
        public int getCount() {
            return 6;
        }

        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int i) {
            Component component = LayoutScatter.getInstance(MainAbility.this)
                    .parse(ResourceTable.Layout_pager_item, null, false);
            componentContainer.addComponent(component);
            return component;
        }

        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer,
                int position, Object object) {
            componentContainer.removeComponent((StackLayout) object);
        }

        @Override
        public boolean isPageMatchToObject(Component component, Object object) {
            return component == object;
        }
    }

    @Override
    protected void onBackPressed() {
        if (pull_to_dismiss_pager.isPanelExpanded()) {
            pull_to_dismiss_pager.collapsePanel();
        } else {
            super.onBackPressed();
        }
    }
}
